package com.hyl.domain.activity.service.discount.impl;

import com.hyl.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.hyl.domain.activity.service.discount.AbstractDiscountCalculateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author hyl
 * @description 满减实现类
 * @create 2025-06-17 20:17
 */
@Slf4j
@Service("MJ")
public class MJCalculateService extends AbstractDiscountCalculateService {
    @Override
    protected BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        String marketExpr = groupBuyDiscount.getMarketExpr();
        // 折扣表达式 - 100,10 满100减10元
        String[] split = marketExpr.split(",");
        BigDecimal targetPrice = new BigDecimal(split[0]);
        BigDecimal discountPrice = new BigDecimal(split[1]);
        // 不满足最低满减约束，则按照原价
        if (originalPrice.compareTo(targetPrice) < 0) {
            return originalPrice;
        }
        BigDecimal deductionPrice = originalPrice.subtract(discountPrice);
        // 判断折扣后金额，最低支付1分钱
        if (deductionPrice.compareTo(BigDecimal.ZERO) < 0){
            return new BigDecimal("0.01");
        }
        return deductionPrice;//折扣价格
    }
}
